package com.inews.base.security.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import com.inews.base.security.Constants;
import com.inews.base.security.repository.SecurityDao;
import com.inews.base.security.vo.ResourceCacheVo;
import com.inews.base.security.vo.TreeNode;
import com.inews.base.utils.InewsUtils;
import com.inews.business.entity.TblBaseResource;

@Service
public class ResourceCacheServiceImpl implements ResourceCacheService{
	
	@Autowired
	private SecurityDao securityDao;
	private static ResourceCacheVo resourceCacheVo;

	/**
	 * 初始化资源.
	 *
	 * @throws Exception the exception
	 */
	@PostConstruct
	public void initResourcesCacheMap() throws Exception{
		if (resourceCacheVo != null){
			return;
		}
		Map<String, TblBaseResource> resPoMap = new HashMap<String, TblBaseResource>();
		List<TblBaseResource> resPoList = securityDao.findTblBaseResourcePo();
		for (TblBaseResource resPo : resPoList) {
			resPoMap.put(resPo.getResourceId(), resPo);
		}
		Map<String, TblBaseResource> actionMap = new HashMap<String, TblBaseResource>();
		resourceCacheVo = new ResourceCacheVo();
		Map<String, TblBaseResource> sourcesMapFinal= new HashMap<String, TblBaseResource>();
		
		for (Map.Entry<String, TblBaseResource> entry : resPoMap.entrySet()) {
			TblBaseResource resPo = entry.getValue();
			//得到资源ID
			String resourceId = resPo.getResourceId();
			//得到资源URL,即action
			String resourceUrl = resPo.getResourceUrl();
			//得到资源显示标示
			String showType = resPo.getShowType();
			if (InewsUtils.isNotEmpty(resourceUrl)){
				sourcesMapFinal.put(resourceId, resPo);
				if((resourceUrl.indexOf(".action") > -1) && Constants.RES_SHOWTYPE_MENU.equals(showType)){
					String actionName = resourceUrl.substring(0, resourceUrl.indexOf(".action"));
					actionMap.put(actionName, resPo);
				}
			}
		}
		resourceCacheVo.setActionResources(actionMap);
		resourceCacheVo.setResources(sourcesMapFinal);
	}
	
	
	/**
	 * 构建菜单树.
	 *
	 * @param authorities the authorities
	 * @return the tree node
	 */
	public TreeNode buileMenuTree(Collection<GrantedAuthority> authorities) {
		Map<String, TreeNode> memuTreeMap = new LinkedHashMap<String, TreeNode>();
		Map<String, TblBaseResource> resPoMap = resourceCacheVo.getResources();
		for (GrantedAuthority authorityPo : authorities) {
			TblBaseResource resPo = resPoMap.get(authorityPo.getAuthority());
			if (resPo != null) {
				if (resPo.getShowType().equals(Constants.RES_SHOWTYPE_MENU)) {
					TreeNode treeNode = new TreeNode();
					List<TreeNode> children = new ArrayList<TreeNode>();
					treeNode.setId(resPo.getResourceId().toLowerCase());
					treeNode.setLeaf(true);
					treeNode.setParentId(resPo.getFatherId().toLowerCase());
					treeNode.setNodeShowName(resPo.getResourceName());
					treeNode.setUrl(resPo.getResourceUrl());
					treeNode.setText(resPo.getResourceName());
					treeNode.setSort(resPo.getSysCode());
					treeNode.setChildren(children);
					memuTreeMap.put(resPo.getResourceId().toLowerCase(),treeNode);
				}
			}
		}
		//添加根节点-1
		TreeNode rootTreeNode = new TreeNode();
		List<TreeNode> children = new ArrayList<TreeNode>();
		rootTreeNode.setId(Constants.TREE_NODE);
		rootTreeNode.setLeaf(true);
		rootTreeNode.setChildren(children);
		memuTreeMap.put(Constants.TREE_NODE, rootTreeNode);
		formatMenuTree(memuTreeMap);
		return memuTreeMap.get(Constants.TREE_NODE);
	}
	
	
	
	/**
	 * 格式化菜单树.
	 *
	 * @param memuTreeMap the memu tree map
	 */
	private void formatMenuTree(Map<String, TreeNode> memuTreeMap) {
		for (Map.Entry<String, TreeNode> entry : memuTreeMap.entrySet()) {
			if (!entry.getKey().equals(Constants.TREE_NODE)) {
				TreeNode treeNode = entry.getValue();
				String parentId = treeNode.getParentId();
				TreeNode parentNode = memuTreeMap.get(parentId);
				if (parentNode != null) {
					parentNode.getChildren().add(treeNode);
					parentNode.setLeaf(false);
				}
			}
		}
	}
	
	
	
}
